// pages/select/select.js
Page({
  data: {  
    courses: [],
    page: 1,
    pageSize: 5,

    totalCourses: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    const mockData = [

       {
        name: 'C语言程序设计',
        price: 5,
        time: '星期一 第1~2节',
        selectedCount: 17,
        description: '',
        enrolled: false
      },
      {
        id: 2,
        name: 'ASP.Net开发',
        price: 2,
        time: '星期五 第5~7节',
        selectedCount: 0,
        description:'' ,
        enrolled: false
      },
      {
        id: 3,
        name: '数据库概论',
        price: 5,
        time: '星期三 第1~2节',
        selectedCount: 0,
        description: '',
        enrolled: false
      },
      {
        id: 4,
        name: 'Photoshop',
        price: 2,
        time: '星期二 第3~4节',
        selectedCount: 0,
        description: '',
        enrolled: false
      },
      {
      id: 5,
      name: '计算机网络',
      price: 5,
      time: '星期四 第5~7节',
      selectedCount: 0,
      description: '',
      enrolled: false
    },
    ];

    const start = (this.data.page - 1) * this.data.pageSize;
    const end = start + this.data.pageSize;

    const currentPageData = mockData.slice(start, end).map(course => ({
      ...course,
      showDetails: false
    }));

    this.setData({
      courses: currentPageData
    });
  },

  // ✅ 展开/收起课程详情的方法
  toggleCourseDetails(e) {
    const courseId = e.currentTarget.dataset.id;

    const updatedCourses = this.data.courses.map(course => {
      if (course.id === courseId) {
        return {
          ...course,
          showDetails: !course.showDetails
        };
      }
      return course;
    });

    this.setData({
      courses: updatedCourses
    });
  },

  enrollCourse(e) {
    const courseId = e.currentTarget.dataset.id;

    const updatedCourses = this.data.courses.map(course => {
      if (course.id === courseId) {
        let tip = '';
        let tipType = '';

        if (course.enrolled) {
          tip = '💡 您已报名过这门课程';
          tipType = 'info';
        } else if (course.selectedCount >= course.maxStudents) {
          tip = '⚠️ 该课程已满员';
          tipType = 'warning';
        } else {
          tip = '✅ 报名成功';
          tipType = 'success';

          return {
            ...course,
            enrolled: true,
            selectedCount: course.selectedCount + 1,
            tip,
            tipType
          };
        }

        // 显示提示并 3 秒后隐藏
        setTimeout(() => {
          const index = this.data.courses.findIndex(c => c.id === courseId);
          if (index !== -1) {
            const newCourses = [...this.data.courses];
            newCourses[index] = { ...newCourses[index], tip: '', tipType: '' };
            this.setData({ courses: newCourses });
          }
        }, 3000);

        return { ...course, tip, tipType };
      }
      return course;
    });

    this.setData({
      courses: updatedCourses
    });
  },

  prevPage() {
    if (this.data.page > 1) {
      this.data.page--;
      this.loadCourses();
    }
  },

  nextPage() {
    const totalPages = Math.ceil(this.data.courses.length / this.data.pageSize);
    if (this.data.page < totalPages) {
      this.data.page++;
      this.loadCourses();
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})